package chessapp;

//
//  ChessApp.java
//  A simple Java Swing applet to play chess.
//  Copyright (c) 2001 Peter Hunter. GUI was originally based on code
//  by David Dagon (c) 1997 David Dagon and is used with his permission.
//  The search code is heavily based on Tom Kerrigan's tscp, for which he
//  owns the copyright, and is used with his permission. All rights are
//  reserved by the owners of the respective copyrights.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

public class ChessApp extends JApplet {
    private Board board = new Board();
    private BoardView brdView;
    int computerSide = Board.DARK;
    private final int[] playTime = {3000, 5000, 10000, 20000, 30000, 60000};
    
    public void init() {
        super.init();
        
        showStatus ("Please Wait; Program Loading");
        BoardView.bufferImages(this);
        
        JPanel p1 = new JPanel();
        p1.setLayout(new FlowLayout(FlowLayout.LEFT));

        //add (flipBox = new Checkbox("Flip Board"));
        JButton resetButton = new JButton ("New Game");
        resetButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                computerSide = Board.DARK;
                playNewGame();
            }
        });
        p1.add(resetButton);    
        
        JButton switchSidesButton = new JButton ("Switch Sides");
        switchSidesButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                computerSide = board.side;
                brdView.computerMove();
            }
        });
        p1.add(switchSidesButton);

        String[] timeStrings = { "3 seconds", "5 seconds", "10 seconds", "20 seconds", "30 seconds", "1 minute" };
        JComboBox timeBox = new JComboBox(timeStrings);
        timeBox.setSelectedIndex(2);
        timeBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JComboBox cb = (JComboBox)e.getSource();
                int selection = cb.getSelectedIndex();
                brdView.setMaxTime(playTime[selection]);
            }
        });
        p1.add(timeBox);
        p1.add(new JLabel("per computer move"));
                
        JPanel p2 = new JPanel();
        brdView = new BoardView(this, board);
        p2.add(brdView);

        Container cp = getContentPane();
        cp.setLayout(new BorderLayout());
        cp.add(p1, "North");
        cp.add(p2, "Center");
    }
    
    public void playNewGame() {
        board = new Board();
        brdView.setBoard(board);
    }
}
